<?php

namespace common\service;

use common\components\HttpClient;

/**
 * 公共的一些方法所在的位置.
 * Class CommonService
 * @package common\service
 */
class CommonService extends BaseService
{
    /**
     * 获取客户端IP地址.
     * @return string
     */
    public static function getIP()
    {
        if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
            return $_SERVER["HTTP_X_FORWARDED_FOR"];
        return isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"]:'';
    }

    /**
     * 分页.
     * @param $params
     * @return array
     */
    public static function ipagination($params)
    {
        $ret       = [
            'previous'  => true,
            'next'      => true,
            'from'      => 0,
            'end'       => 0,
            'total_page' => 0,
            'current'   => 0,
            'page_size' => intval( $params['page_size'] ),
            'offset'   => 0,
        ];
        $total     = (int)$params['total_count'];
        $pageSize  = (int)$params['page_size'];
        $page      = (int)$params['page'];
        $display   = (int)$params['display'];
        $totalPage = (int)ceil($total / $pageSize);
        $totalPage = $totalPage?$totalPage:1;

        if ($page <= 1) {
            $ret['previous'] = false;
        }
        if ($page >= $totalPage) {
            $ret['next'] = false;
        }
        $semi = (int)ceil($display / 2);
        if ($page - $semi > 0) {
            $ret['from'] = $page - $semi;
        } else {
            $ret['from'] = 1;
        }
        if ($page + $semi <= $totalPage) {
            $ret['end'] = $page + $semi;
        } else {
            $ret['end'] = $totalPage;
        }
        $ret['total_count'] = $total;
        $ret['total_page'] = $totalPage;
        $ret['current']   = $page;
        $ret['offset'] = max([0, ($page - 1) * $ret['page_size']]);
        return $ret;
    }

    /**
     * 展示订单状态
     * @param int $status
     * @return string
     */
    public static function getOrderStatus($status = 0){
        switch ($status){
            case 0:
            return "待付款";break;
            case 1:
            return "待发货";break;
            case 2:
            return "已发货";break;
            case 3:
            return "待收货";break;
            case 4:
            return "申请退款";break;
            case 5:
            return "已完成";break;
            case 6:
            return "已关闭";break;
            case 7:
            return "成团中";break;
        }
    }

    /**
     * 随机数
     * @param int $length
     * @param int $numeric
     * @return string
     */
    public static function random($length = 6 , $numeric = 0) {
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        if($numeric) {
            $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash = '';
            $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
        }
        return $hash;
    }

    /**
     * 生成订单号
     * @param string $prefix
     * @return string
     */
    public static function createOrderNo($prefix = 'shop'){
        $no = $prefix.date('Ymd').uniqid();
        return $no;
    }


    /**
     * 验签
     * @param array $result
     * @return bool
     */
    public static  function checkSign(array $result = [])
    {
        try{
            $sign = $result['sign'];

            unset($result['sign']);

            ksort($result);

            $result['key'] = \Yii::$app->params['wechat']['key'];

            if($sign != strtoupper(md5(http_build_query($result))))
            {
                return false;
            }
            return true;
        }catch (\Exception $e){
            return false;
        }
    }


    /**
     * 将array转为xml
     * @param $arr
     * @return string
     */
    public static function arrayToXml($arr){
        $xml = "<xml>";
        foreach ($arr as $key=>$val){
            if (is_numeric($val)){
                $xml.="<".$key.">".$val."</".$key.">";

            }else{
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
        return $xml;
    }

    /**
     *  作用：将xml转为array
     */
    public static function xmlToArray($xml){
        //将XML转为array
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $array_data;
    }



    /**
     * 小程序倒计时格式化
     */
    public static function Timecountdown($beg,$end){
        $complete = $end-$beg;
        $hours = floor($complete/3600);

        $minute = floor(($complete-$hours*60*60)/60);

        $sec = floor($complete-($minute*60+$hours*60*60));
        $data['hours'] = $hours;
        $data['minute'] = $minute;
        $data['sec'] = $sec;
        return $data;
    }

    /**
     * 展示提现状态
     * @param int $status
     * @return string
     */
    public static function getWithdrawStatus($status = 0){
        switch ($status){
            case 0:
                return "待审核";break;
            case 1:
                return "未通过";break;
            case 2:
                return "提现中";break;
            case 3:
                return "已完成";break;
            case 4:
                return "提现失败";break;
        }
    }



}